//
//  CarQueryHomeViewCell.m
//  XTCloud
//
//  Created by QA on 2017/3/2.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CarQueryHomeViewCell.h"
#import "NSDictionary+DataFormat.h"

@interface CarQueryHomeViewCell()

@property (nonatomic, assign) BOOL isChangeWidth;

@end

@implementation CarQueryHomeViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier index:(NSInteger)index
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CarQueryHomeViewCell" owner:self options:nil];
        self = [topLevelObjects objectAtIndex:index];
        if(index==0) self.isChangeWidth = YES;
    }
    return self;
}

- (instancetype)initWithChangeWidthStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.isChangeWidth = YES;
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    if (self.isChangeWidth) {
        frame.origin.x += 8;
        frame.size.width = SuperWidth-16;
    }
    [super setFrame:frame];
}

//绘制生产线信息
- (void)drawLineViewContent:(NSArray*)lineArray target:(id)target action:(SEL)action
{
    NSArray *array = self.contentView.subviews;
    for(UIView *line in array) [line removeFromSuperview];
    
    self.frame = CGRectMake(0,0, SuperWidth, 150);
    if(!lineArray) lineArray = [NSMutableArray array];
    NSInteger dataCount = lineArray.count;
    
    //加载进度条
    if(dataCount <= 0){
        UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(SuperWidth/2-50/2, (150-60)/2, 50, 50)];
        act.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhite;
        [act startAnimating];
        [self.contentView addSubview:act];
    }
    
    for(int i=0;i<dataCount;i++){
        NSDictionary *dictionary = lineArray[i];
        NSString *scxString = [dictionary objectFormatStringForKey:@"生产线" defaults:@"#"];
        NSString *dqztString = [dictionary objectFormatStringForKey:@"状态" defaults:@"等待装车"];
        NSString *dqchString = [dictionary objectFormatStringForKey:@"正装车号" defaults:@"--"];
        NSString *zbchString = [dictionary objectFormatStringForKey:@"准备车号" defaults:@"暂无"];
        
        UIView *lineView = [[UIView alloc] initWithFrame:
                            CGRectMake(SuperWidth/dataCount*i, (150-98)/2, SuperWidth/dataCount, 95)];
        [self.contentView addSubview:lineView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:target action:action];
        lineView.tag = i;
        [lineView addGestureRecognizer:tapGesture];
        
        UILabel *titleText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, lineView.frame.size.width, 20)];
        titleText.font = [UIFont systemFontOfSize:16];
        //titleText.text = [NSString stringWithFormat:@"生产线%@",scxString];
        titleText.text = [NSString stringWithFormat:@"%@",scxString];
        titleText.textColor = [UIColor whiteColor];
        titleText.textAlignment = NSTextAlignmentCenter;
        [lineView addSubview:titleText];
        
        UILabel *stateText = [[UILabel alloc] initWithFrame:CGRectMake(0, titleText.frame.origin.y+titleText.bounds.size.height, lineView.frame.size.width, 20)];
        stateText.font = [UIFont systemFontOfSize:11];
        stateText.text = dqztString;
        stateText.textColor = [UIColor whiteColor];
        stateText.textAlignment = NSTextAlignmentCenter;
        [lineView addSubview:stateText];
        
        UILabel *lineCodeText = [[UILabel alloc] initWithFrame:CGRectMake(0, stateText.frame.origin.y+stateText.bounds.size.height, lineView.frame.size.width, 35)];
        lineCodeText.font = [UIFont boldSystemFontOfSize:24];
        lineCodeText.textAlignment = NSTextAlignmentCenter;
        lineCodeText.text = dqchString;
        lineCodeText.textColor = [UIColor whiteColor];
        [lineView addSubview:lineCodeText];
        
        UILabel *prepareText = [[UILabel alloc] initWithFrame:CGRectMake(2, lineCodeText.frame.origin.y+lineCodeText.bounds.size.height, lineView.frame.size.width - 4, 30)];
        prepareText.font = [UIFont systemFontOfSize:10];
        prepareText.text = [NSString stringWithFormat:@"准备：%@",zbchString];
        prepareText.lineBreakMode = NSLineBreakByWordWrapping;
        prepareText.numberOfLines = 3;
        prepareText.textAlignment = NSTextAlignmentCenter;
        prepareText.textColor = [UIColor whiteColor];
        [lineView addSubview:prepareText];
    }
    
}

//绘制车辆信息
- (void)drawCarViewContent:(NSDictionary*)dictionary
{
    NSInteger frameWidth = SuperWidth-16;
    
    NSString *rwdbh = [dictionary objectFormatStringForKey:@"任务单编号" defaults:@"无"];
    NSString *gcmc = [dictionary objectFormatStringForKey:@"工程名称" defaults:@"无工程名称"];
    NSString *jhfl = [dictionary objectFormatStringForKey:@"计划方量" defaults:@"-"];
    NSString *yffl = [dictionary objectFormatStringForKey:@"累计方量" defaults:@"-"];
    NSString *cc = [dictionary objectFormatStringForKey:@"已发车次" defaults:@"-"];
    
    UIView *spaceTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 10)];
    spaceTop.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:spaceTop];
    
    UILabel *rwdbhText = [[UILabel alloc] initWithFrame:CGRectMake(5, 11, frameWidth - 10, 22)];
    rwdbhText.font = [UIFont systemFontOfSize:15];
    rwdbhText.text = rwdbh;
    rwdbhText.textColor = [UIColor colorWithWhite:0.1 alpha:1];
    [self.contentView addSubview:rwdbhText];
    
    UILabel *titleText = [[UILabel alloc] initWithFrame:CGRectMake(5, 28, frameWidth - 10, 22)];
    titleText.font = [UIFont systemFontOfSize:13];
    titleText.text = gcmc;
    titleText.textColor = [UIColor colorWithWhite:0.1 alpha:1];
    [self.contentView addSubview:titleText];
    
    //添加分割线
    UIView *lineViewTop = [[UIView alloc] initWithFrame:CGRectMake(0, 50, frameWidth, 0.5)];
    lineViewTop.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.25];
    [self.contentView addSubview:lineViewTop];
    
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CarQueryHomeViewCell" owner:self options:nil];
    UIView *itemCarView = [topLevelObjects objectAtIndex:1];
    NSInteger colCount = frameWidth / itemCarView.frame.size.width;
    
    NSArray *itemArray = [dictionary objectForKey:@"车辆信息"];
    NSInteger dataCount = itemArray.count;//数据条数
    CGFloat viewFrameHeight = (dataCount/colCount + (dataCount%colCount==0 ? 0 : 1)) * itemCarView.frame.size.height;

    for(int i=0;i<dataCount;i++){
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CarQueryHomeViewCell" owner:self options:nil];
        UIView *itemCarView = [topLevelObjects objectAtIndex:1];
        [self.contentView addSubview:itemCarView];
        
        NSDictionary *dic = [itemArray objectAtIndex:i];
        UILabel *carNumberLabel = [itemCarView viewWithTag:1000];
        carNumberLabel.text = [dic objectFormatStringForKey:@"车号"];
        
        CGRect viewFrame = itemCarView.frame;
        viewFrame.origin.y = 50 + i/colCount*viewFrame.size.height;
        viewFrame.origin.x = i%colCount*viewFrame.size.width;
        itemCarView.frame = viewFrame;
    }
    
    //添加分割线
    UIView *lineViewBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 55.5+viewFrameHeight, frameWidth, 0.5)];
    lineViewBottom.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.25];
    [self.contentView addSubview:lineViewBottom];
    
    UILabel *describeText = [[UILabel alloc] initWithFrame:CGRectMake(5, 55+viewFrameHeight, frameWidth-10, 30)];
    describeText.font = [UIFont systemFontOfSize:12];
    describeText.text = [NSString stringWithFormat:@"计划方量%@方，已发送%@方，已发出%@车",jhfl,yffl,cc];
    describeText.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:describeText];
}

+ (CGFloat)getCellHeight:(NSInteger)dataCount
{
    NSInteger frameWidth = SuperWidth-16;
    
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CarQueryHomeViewCell" owner:self options:nil];
    UIView *itemCarView = [topLevelObjects objectAtIndex:1];
    NSInteger colCount = frameWidth / itemCarView.frame.size.width;
    CGFloat viewFrameHeight = (dataCount/colCount + (dataCount%colCount==0 ? 0 : 1)) * itemCarView.frame.size.height;
    
    return 55.5+viewFrameHeight+30.5;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
